﻿--[[
   @brief    suggest predownload tips model
   @author   leozzyzheng
   @version  1.0
   @date     2015-03-26
--]]

suggest_predownload_model = {
    -- cfg_url = "http://119.147.201.178/fcg-bin/general_cfg_query.fcg";
    cfg_url = LoadStr("GENERAL_CFG_QUERY_URL");
    cfg_version = 1;
    cfg_pkg_name = "/tgp/platform/suggest_predownload_tips";
    cfg_name = "calculate_rules";
    cfg_call_back = "suggest_predownload_tips_call_back";
    
    --[[
        计算数据 s = a + b * (now - formal_update_time); 
        now - formal_update_time < max_time
{
    "1": {},
    "2": {},
    "26": {},
    "49": {"a":12351,"b":19},
    "58": {"a":12351,"b":19},
    "36": {"a":12351,"b":19},
    "30": {"a":12351,"b":19},
    "default": {
        "a": 123531,
        "b": 51,
        "max_time": 259200
    }
}
    ]]
    cfg_data = {};
    
    is_valied = false;
}

function suggest_predownload_model:query_cfg()
    -- 请求后台配置项
    local post_data = {};
    post_data["version"] = self.cfg_version;
    post_data["pkg_name"] = self.cfg_pkg_name;
    post_data["cfg_list"] = self.cfg_name;
    post_data["callback"] = self.cfg_call_back;
    base_utility.curl_request_async(self.cfg_url, "suggest_predownload_model.on_cfg_rsp", false, post_data, true);
end;

function suggest_predownload_model.on_cfg_rsp(result)
    base_utility.log("[suggest_predownload_model]received cfg:" .. result);
    local success, result_table, crc = base_utility.cfg_string_to_table(result, suggest_predownload_model.cfg_call_back);
    if success 
       and result_table[1] ~= nil 
       and result_table[1]["cfg_value"] ~= nil then
        result_table = result_table[1]["cfg_value"];
        result_table = base_utility.json_to_table(result_table);
        if result_table ~= nil and result_table.default ~= nil then
            local default = result_table.default;
            if default.a ~= nil and type(default.a) == "number" and
               default.b ~= nil and type(default.b) == "number" and
               default.max_time ~= nil and type(default.max_time) == "number" then
               suggest_predownload_model.is_valied = true;
               suggest_predownload_model.cfg_data = result_table;
            end;
        end;
    end;
end;

-- 通过game_id计算出来有多少人开启了预下载
-- 如果找不到game_id算出来的是0
function suggest_predownload_model:get_number(game_id)
    if not self.is_valied then
        return 0;
    end;

    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    if game_lib ~= nil then
        local timestamp = game_lib:GetFormalUpdateTimeStamp(game_id);
        if timestamp ~= 0 then
            local t = os.time() - timestamp;
            if t > self.cfg_data.default.max_time then
                t = self.cfg_data.default.max_time;
            else
                t = math.abs(t);
            end;
            local game_id_str = tostring(game_id);
            if self.cfg_data[game_id_str] ~= nil then
                local a = self.cfg_data[game_id_str].a or self.cfg_data.default.a;
                local b = self.cfg_data[game_id_str].b or self.cfg_data.default.b;
                return a + b * t;
            end;
        end;
    end;
    
    return 0;
end;

function suggest_predownload_model:check_valied()
    return self.is_valied;
end;

